﻿using System;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Entities = Vision.Core.Entities;
using Components = Vision.Core.Components;

namespace Vision.Web.Pages
{
    public partial class Activities : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack){
                using (Components.ActivityCatalog activityCatalog = new Components.ActivityCatalog(ConfigurationManager.AppSettings["VisionCatalog"])){
                    this.tvVisionFolders.Nodes.Add(BuildNode(activityCatalog.GetFolders()));
                    this.tvVisionFolders.DataBind();
                    this.gvVisionActivities.DataSource = null;
                    this.gvVisionActivities.DataBind();
                }                
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void tvVisionFolders_SelectedNodeChanged(object sender, EventArgs e)
        {
            int folderId = int.Parse(this.tvVisionFolders.SelectedValue);            
            using (Components.ActivityCatalog activityCatalog = new Components.ActivityCatalog(ConfigurationManager.AppSettings["VisionCatalog"])){
                this.gvVisionActivities.DataSource = activityCatalog.GetVisionActivities(folderId).OrderBy(fi => fi.Name);
                this.gvVisionActivities.DataBind();
            }            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvVisionActivitiesAction_Click(object sender, ImageClickEventArgs e)
        {            
            ImageButton button = (ImageButton)sender;
            if (button != null){

                Common.QStringBuilder qsBuilder = new Common.QStringBuilder();
                
                switch (button.CommandName){
                    case "Log":
                        qsBuilder.Path = "~/Pages/ActivityLog.aspx";
                        qsBuilder.Parameters.Clear();
                        qsBuilder.Parameters.Add("ActivityId", button.CommandArgument);                        
                        Response.Redirect(qsBuilder.QueryString);                        
                        break;
                    case "Schedule":
                        qsBuilder.Parameters.Clear();
                        qsBuilder.Path = "~/Pages/ActivitySchedule.aspx";
                        qsBuilder.Parameters.Add("ActivityId", button.CommandArgument);                        
                        Response.Redirect(qsBuilder.QueryString);                        
                        break;
                    case "Queue":
                        int activityId = int.Parse(button.CommandArgument);
                        using (Components.ActivityCatalog activityCatalog = new Components.ActivityCatalog(ConfigurationManager.AppSettings["VisionCatalog"])){
                            Entities.Activities.VisionActivity activity = activityCatalog.GetVisionActivityFromCatalog(activityId);
                            using (Components.ActivityQueueSender queueSender = new Components.ActivityQueueSender(ConfigurationManager.AppSettings["VisionQueue"])){
                                Entities.Activities.VisionActivityMessage vaMessage = new Entities.Activities.VisionActivityMessage();
                                vaMessage.ActivityKey = activity.Key;
                                vaMessage.ActivityParams = new Entities.Activities.VisionActivityParameterCollection();
                                queueSender.SendVisionActivityMessage(vaMessage);
                            }                                                        
                        }                
                        
                        qsBuilder.Parameters.Clear();
                        qsBuilder.Path = "~/Pages/Activities.aspx";                        
                        Response.Redirect(qsBuilder.QueryString);                        
                        break;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="folder"></param>
        /// <returns></returns>
        private TreeNode BuildNode(Entities.Filing.Folder folder)
        {
            TreeNode node = new TreeNode();
            node.Value = folder.Id.ToString();
            node.Text = folder.Name;
            foreach (Entities.Filing.Folder fld in folder.Folders){
                node.ChildNodes.Add(BuildNode(fld));
            }
            return node;
        }
    }
}